package de.haw.ai3.bs.aufgabe3.a1.vonAndre;

import java.util.Collections;
import java.util.HashSet;
import java.util.concurrent.Semaphore;

public class Mensa {

	private HashSet<Kasse> kassenListe = new HashSet<>();
	private HashSet<Student> studenten = new HashSet<>();

	private Semaphore accessControl;

	public Mensa(Kasse[] kassen, Student[] studis) throws InterruptedException {
		for (Kasse kasse : kassen) {
			kassenListe.add(kasse);
		}
		accessControl = new Semaphore(1, true);
		accessControl.acquire();

		for (Student student : studis) {
			studenten.add(student);
		}
		for (Student student2 : studenten) {
			student2.betreteMensa(this);
		}
		accessControl.release();
	}

	/**
	 * Zuweiser weist Student Kasse zu
	 * 
	 * @param student
	 * @return
	 * @throws InterruptedException
	 */
	public Kasse anstellen(Student student) throws InterruptedException {
		accessControl.acquire();
		Kasse kasse = Collections.min(kassenListe);
		accessControl.release();

		kasse.anstellen(student);
		return kasse;
	}

}
